package crmdna.interaction;

import java.util.Collections;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;

import com.googlecode.objectify.annotation.Cache;
import com.googlecode.objectify.annotation.Entity;
import com.googlecode.objectify.annotation.Id;
import com.googlecode.objectify.annotation.Index;
import com.googlecode.objectify.annotation.Serialize;

@Entity
@Cache
public class InteractionEntity {
	@Id
	long interactionId;

	@Serialize(zip = true)
	Map<Long, String> subInteractions = new HashMap<>();

	// key is nano seconds, value is sub interaction content
	// map is always descending sorted by key

	@Index
	long ms;

	@Index
	String interactionType;

	@Index
	long userId;

	String user; // user name

	@Index
	long memberId;

	public InteractionProp toProp() {
		InteractionProp interactionProp = new InteractionProp();

		interactionProp.interactionId = interactionId;
		interactionProp.timestamp = new Date(ms);

		for (Long subinteractionId : subInteractions.keySet()) {
			SubInteractionProp subInteractionProp = new SubInteractionProp();
			subInteractionProp.subInteractionId = subinteractionId;
			subInteractionProp.content = subInteractions.get(subinteractionId);
			subInteractionProp.timestamp = new Date(subinteractionId / 1000000);
			interactionProp.subInteractionProps.add(subInteractionProp);
		}
		Collections.sort(interactionProp.subInteractionProps);

		interactionProp.interactionType = interactionType;
		interactionProp.user = user;
		interactionProp.memberId = memberId;

		return interactionProp;
	}
}
